home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / DirectionButton.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  7.3 KB  |  292 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.awt.Rectangle;
  5. import java.awt.Dimension;
  6. import java.awt.Color;
  7. import java.awt.Graphics;
  8. import java.awt.Polygon;
  9.  
  10. //    01/29/97    TWB    Integrated changes from Windows 
  11.  
  12. /* *
  13.  * DirectionButton is a button component that has an arrow drawn in it that
  14.  * points one of four ways (left,up,right,down).
  15. */
  16.  
  17. /**
  18.  * This component creates an arrow and is an extension of  ButtonBase. At 
  19.  * runtime, the button has an up look and pressed look.
  20.  * <p>
  21.  * This component is usually used in conjunction with a combo or list box to 
  22.  * indicate a list that the user can view by clicking the arrow.
  23.  * <p>
  24.  * To create an interaction with another component, use the Interaction Wizard. 
  25.  * <p>
  26.  * @version 1.0, Nov 26, 1996
  27.  * @author  Symantec
  28.  */
  29. public class DirectionButton
  30.     extends ButtonBase
  31. {
  32.     /**
  33.      * The point LEFT style constant.
  34.      */
  35.     public static final int LEFT = 0;
  36.  
  37.     /**
  38.      * The point RIGHT style constant.
  39.      */
  40.     public static final int RIGHT = 1;
  41.  
  42.     /**
  43.      * The point UP style constant.
  44.      */
  45.     public static final int UP = 2;
  46.  
  47.     /**
  48.      * The point DOWN style constant.
  49.      */
  50.     public static final int DOWN = 3;
  51.  
  52.  
  53.     private int     direction;
  54.     private int     left;
  55.     private int     right;
  56.     private int     top;
  57.     private int     bottom;
  58.     private int     indent;
  59.     private Polygon poly;
  60.  
  61.  
  62.     /**
  63.      * Constructs a default Direction Button which will point left.
  64.      */
  65.     public DirectionButton()
  66.     {
  67.         this(LEFT);
  68.     }
  69.  
  70.     /**
  71.      * Constructs a Direction Button pointing the specified direction.
  72.      * @param d style constant indicating direction to point button
  73.      * @see #LEFT
  74.      * @see #UP
  75.      * @see #RIGHT
  76.      * @see #DOWN
  77.      */
  78.     public DirectionButton(int d)
  79.     {
  80.         direction = d;
  81.         left      = 0;
  82.         right     = 0;
  83.         bottom    = 0;
  84.         indent    = 0;
  85.         poly      = null;
  86.     }
  87.  
  88.     /**
  89.      * Sets the direction of the arrow after construction.
  90.      * @param d constant indicating direction to point button
  91.      * @see #getDirection
  92.      * @see #LEFT
  93.      * @see #UP
  94.      * @see #RIGHT
  95.      * @see #DOWN
  96.      */
  97.     public void setDirection(int d)
  98.     {
  99.         direction = d;
  100.     }
  101.  
  102.     /**
  103.      * Returs the direction the button is currently pointing.
  104.      * @see #setDirection
  105.      * @see #LEFT
  106.      * @see #UP
  107.      * @see #RIGHT
  108.      * @see #DOWN
  109.      */
  110.     public int getDirection()
  111.     {
  112.         return direction;
  113.     }
  114.  
  115.     /**
  116.      * Sets the size of blank space between the arrow and the button 
  117.      * border in pixels.
  118.      * @param ai margin around arrow in pixels. 0=arrow takes up entire button
  119.      * @see #getArrowIndent
  120.      */
  121.     public void setArrowIndent(int ai)
  122.     {
  123.         indent = ai;
  124.         invalidate();
  125.     }
  126.  
  127.     /**
  128.      * Reurns the size of blank space between the arrow and the button 
  129.      * border in pixels.
  130.      * @see #setArrowIndent
  131.      */
  132.     public int getArrowIndent()
  133.     {
  134.         return indent;
  135.     }
  136.  
  137.     /**
  138.      * Sets the extra amount in pixels to shrink triangle
  139.      * @param left pixels to shrink from left side
  140.      * @param right pixels to shrink from right side
  141.      * @param top pixels to shrink from top
  142.      * @param bottom pixels to shrink from bottom
  143.      */
  144.     public void shrinkTriangle(int l, int r, int t, int b)
  145.     {
  146.         left   = l;
  147.         right  = r;
  148.         top    = t;
  149.         bottom = b;
  150.     }
  151.  
  152.     /**
  153.      * Paints this component using the given graphics context.
  154.      * This is a standard Java AWT method which typically gets called
  155.      * by the AWT to handle painting this component. It paints this component
  156.      * using the given graphics context. The graphics context clipping region
  157.      * is set to the bounding rectangle of this component and its <0,0>
  158.      * coordinate is this component's top-left corner.
  159.      * 
  160.      * @param g the graphics context used for painting
  161.      * @see java.awt.Component#repaint
  162.      * @see symantec.itools.awt.ButtonBase#update
  163.      */
  164.     public void paint(Graphics g)
  165.     {
  166.         super.paint(g);
  167.  
  168.         updatePolygon();
  169.  
  170.         if(isEnabled())
  171.         {
  172.             g.setColor(Color.black);
  173.         }
  174.         else
  175.         {
  176.             g.setColor(Color.gray);
  177.         }
  178.  
  179.         g.fillPolygon(poly);
  180.     }
  181.  
  182.     /**
  183.      * Returns the recommended dimensions to properly display this component.
  184.      * This is a standard Java AWT method which gets called to determine
  185.      * the recommended size of this component. 
  186.      *
  187.      * @see java.awt.Component#minimumSize
  188.      */
  189.     public Dimension preferredSize()
  190.     {
  191.         Dimension s;
  192.  
  193.         s = size();
  194.  
  195.         return new Dimension(Math.max(s.width, minimumSize().width), Math.max(s.height, minimumSize().height));
  196.     }
  197.  
  198.     void updatePolygon()
  199.     {
  200.         Dimension s;
  201.         int centerHorizontal;
  202.         int centerVertical;
  203.         int topSide;
  204.         int bottomSide;
  205.         int leftSide;
  206.         int rightSide;
  207.  
  208.         s    = size();
  209.         poly = new Polygon();
  210.  
  211.         centerHorizontal = (s.width / 2)                   + pressedAdjustment;
  212.         centerVertical   = (s.height / 2)                  + pressedAdjustment;
  213.         topSide          = (top + bevel * 2)               + pressedAdjustment  + indent;
  214.         bottomSide       = (s.height - bottom - bevel * 2) + pressedAdjustment  - indent;
  215.         leftSide         = (left + bevel * 2)              + pressedAdjustment  + indent;
  216.         rightSide        = (s.width - right - bevel * 2)   + pressedAdjustment  - indent;
  217.  
  218.         switch (direction)
  219.         {
  220.             // -1, -2 etc... added 12/11/96 - Andy 
  221.             case UP:
  222.             {
  223.                   if (symantec.itools.lang.OS.isMacintosh())
  224.                   { 
  225.                     poly.addPoint(centerHorizontal-1, topSide-1);
  226.                     poly.addPoint(leftSide-1, bottomSide-2);
  227.                     poly.addPoint(rightSide-2, bottomSide-2);
  228.                 }
  229.                 else
  230.                 {
  231.                     poly.addPoint(centerHorizontal, topSide);
  232.                     poly.addPoint(leftSide, bottomSide);
  233.                     poly.addPoint(rightSide, bottomSide);
  234.                 }
  235.                 break;
  236.             }
  237.  
  238.             case DOWN:
  239.             {
  240.                   if (symantec.itools.lang.OS.isMacintosh())
  241.                   { 
  242.                     poly.addPoint(centerHorizontal-1, bottomSide);
  243.                     poly.addPoint(leftSide-1, topSide);
  244.                     poly.addPoint(rightSide-1, topSide);
  245.                 }
  246.                 else
  247.                 {
  248.                     poly.addPoint(centerHorizontal, bottomSide);
  249.                     poly.addPoint(leftSide, topSide);
  250.                     poly.addPoint(rightSide, topSide);
  251.                 }
  252.                 break;
  253.             }
  254.  
  255.             case LEFT:
  256.             {
  257.                   if (symantec.itools.lang.OS.isMacintosh())
  258.                   { 
  259.                     poly.addPoint(leftSide-2, centerVertical-1);
  260.                     poly.addPoint(rightSide-2, topSide-1);
  261.                     poly.addPoint(rightSide-2, bottomSide-1);
  262.                 }
  263.                 else
  264.                 {
  265.                     poly.addPoint(leftSide, centerVertical);
  266.                     poly.addPoint(rightSide, topSide);
  267.                     poly.addPoint(rightSide, bottomSide);
  268.                 }
  269.                 break;
  270.             }
  271.  
  272.             case RIGHT:
  273.             {
  274.                   if (symantec.itools.lang.OS.isMacintosh())
  275.                   { 
  276.                     poly.addPoint(rightSide-1, centerVertical-1);
  277.                     poly.addPoint(leftSide, topSide-1);
  278.                     poly.addPoint(leftSide, bottomSide-2);
  279.                 }
  280.                 else
  281.                 {
  282.                     poly.addPoint(rightSide, centerVertical);
  283.                     poly.addPoint(leftSide, topSide);
  284.                     poly.addPoint(leftSide, bottomSide);
  285.                 }
  286.                 break;
  287.             }
  288.         }
  289.     }
  290. }
  291.  
  292.